import { Message } from 'iview';
import { findRole, getAllModule, findPermissionByRole, addRolePermission, removeRolePermission } from '../../api/ServerApi';

const RolesPermission = {
    namespaced: true,
    state() {
        return {
            pageLoading: false,
            allModule: [],
            queryRolesParams: {
                pageNo: 1,
                pageSize: 10,
                name: null,
                createStart: null,
                createEnd: null
            },
            rolesTableLoading: false,
            rolesTableData: {
                list: [],
                total: 0,
                pageNum: 1,
                pageSize: 10
            },
            currentRole: null,

            queryPermissionParams: {
                pageNo: 1,
                pageSize: 15,
                roleName: null,
                module: null,
                title: null,
                permission: null,
                resourcesType: '2'
            },
            permissionTableLoading: false,
            permissionTableData: {
                list: [],
                total: 0,
                pageNum: 1,
                pageSize: 15
            }
        };
    },
    mutations: {
        setAllModule(state, allModule) {
            state.allModule = allModule;
        },
        setPageLoading(state, pageLoading) {
            // state.pageLoading = pageLoading;
        },
        setRolesTableLoading(state, rolesTableLoading) {
            state.rolesTableLoading = rolesTableLoading;
        },
        setRolesTableData(state, rolesTableData) {
            state.rolesTableData = rolesTableData;
        },
        setPermissionTableLoading(state, permissionTableLoading) {
            state.permissionTableLoading = permissionTableLoading;
        },
        setPermissionTableData(state, permissionTableData) {
            state.permissionTableData = permissionTableData;
        },
        setCurrentRole(state, currentRole) {
            state.currentRole = currentRole;
        }
    },
    actions: {
        setCurrentRole({ commit, state, dispatch }, currentRole, oldRole) {
            commit('setCurrentRole', currentRole);
            dispatch('queryPermissionByRole');
        },
        getAllModule({ commit, state }) {
            commit('setPageLoading', true);
            getAllModule().then(allModule => {
                commit('setAllModule', allModule);
                commit('setPageLoading', false);
            }).catch((error) => commit('setPageLoading', false));
        },
        queryRolesData({ commit, state }, initPage) {
            if (initPage === true) {
                state.queryRolesParams.pageNo = 1;
            }
            commit('setCurrentRole', null);
            commit('setPermissionTableData', { ...state.permissionTableData, list: [], total: 0, pageNum: 1 });
            commit('setRolesTableLoading', true);
            findRole(state.queryRolesParams).then(data => {
                if (data.pageNum <= 0) {
                    data.pageNum = 1;
                }
                commit('setRolesTableData', data);
                commit('setRolesTableLoading', false);
            }).catch((error) => commit('setRolesTableLoading', false));
        },
        queryPermissionByRole({ commit, state }, initPage) {
            if (!state.currentRole || !state.currentRole.name) {
                Message.warning({ content: '请先选中角色', duration: 3 });
                return;
            }
            if (initPage === true) {
                state.queryPermissionParams.pageNo = 1;
            }
            state.queryPermissionParams.roleName = state.currentRole.name;
            commit('setPermissionTableLoading', true);
            findPermissionByRole(state.queryPermissionParams).then(data => {
                if (data.pageNum <= 0) {
                    data.pageNum = 1;
                }
                commit('setPermissionTableData', data);
                commit('setPermissionTableLoading', false);
            }).catch((error) => commit('setPermissionTableLoading', false));
        },
        addOrRomveRolePermission({ commit, state, dispatch }, { exists, row }) {
            if (!state.currentRole || !state.currentRole.name) {
                Message.warning({ content: '请先选中角色', duration: 3 });
                return;
            }
            if (exists === true) {
                commit('setPageLoading', true);
                addRolePermission({ roleName: state.currentRole.name, permission: row.permission }).then(data => {
                    dispatch('queryPermissionByRole');
                    commit('setPageLoading', false);
                    Message.success({ content: '授权成功', duration: 3 });
                }).catch((error) => commit('setPageLoading', false));
            }
            if (exists === false) {
                commit('setPageLoading', true);
                removeRolePermission(state.currentRole.name, row.permission).then(data => {
                    dispatch('queryPermissionByRole');
                    commit('setPageLoading', false);
                    Message.success({ content: '取消授权成功', duration: 3 });
                }).catch((error) => commit('setPageLoading', false));
            }
        }
    }
};

export default RolesPermission;
